//
//  ShareMessageTableViewController.m
//  Umoney
//
//  Created by zwz on 16/8/10.
//  Copyright © 2016年 zhangwz. All rights reserved.
//

#import "ShareMessageTableViewController.h"
#import "ShareMessageTableViewCell.h"

@interface ShareMessageTableViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    UITableView *_MessageTableView;
    NSString *UID;
    NSArray *res;
}

@end

@implementation ShareMessageTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self CreateTableView];
    
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    UID = [user objectForKey:@"uid"];
    
    [self Getdata];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)CreateTableView
{
    _MessageTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 30, self.view.frame.size.width, self.view.frame.size.height-42) style:UITableViewStyleGrouped];
    _MessageTableView.delegate =self;
    _MessageTableView.dataSource =self;
    [self.view addSubview:_MessageTableView];
}


#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [res count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 130;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *cellID = @"ShareMessageTableViewCell";
    ShareMessageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    cell.backgroundColor=nil;
    if (!cell) {
        cell = [[[NSBundle mainBundle] loadNibNamed:cellID owner:self options:nil] lastObject];
    }
    
    cell.FriendName.text = [res[indexPath.row] valueForKey:@"sfname"];
    cell.ShareText.text =[res[indexPath.row] valueForKey:@"smessage"];
    
    
    return cell;
}

-(void)Getdata
{
    NSString *urlString = [NSString stringWithFormat:@"http://23.106.134.160/umoney/show_messages.php"];
    
    NSURLSessionConfiguration *config = [NSURLSessionConfiguration ephemeralSessionConfiguration];
    config.timeoutIntervalForRequest = 30.0f;
    config.timeoutIntervalForResource = 60.0f;
    NSURLSession *session = [NSURLSession sessionWithConfiguration:config];
    NSURLSessionTask *task = [session dataTaskWithURL:[NSURL URLWithString:urlString] completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        res = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
        
        [_MessageTableView reloadData];	
        
    }];
    [task resume];
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
